package graphicUserInterface.elements;

import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.io.IOException;

import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.protocol.DataSource;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import engine.exceptions.NoWebcamFoundException;
import engine.videoCapture.ConfigureWebcam;

public class WebCamDisplay extends JLayeredPane{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1594735405805338754L;

	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private Player _ply;
	//private HINBRect _calibrationArea;
	private JPanel _calibrationArea;
	private JPanel _TrackerSquare;
	
	private static final Integer webcamZidx = new Integer(0);
	private static final Integer trackingZidx = new Integer(2);
	private static final Integer calibrationZidx = new Integer(10);
	
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	public WebCamDisplay(){
		super();
		System.out.println("WebCamDisplay creation");
		//this.setLayout(null);
		this.setSize(640, 480);
		//this.setSize(320, 240);
		this.setPreferredSize(this.getSize());
		DataSource data;
		this._ply = null;
		try {
			data = ConfigureWebcam.doConfigureWebcam();
			_ply = Manager.createRealizedPlayer(data);
		} catch (NoWebcamFoundException e) {
			e.printStackTrace();
		} catch (NoPlayerException e) {
			e.printStackTrace();
		} catch (CannotRealizeException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(_ply == null){
				this.add(new JLabel("No WebCam Device working found ..."));
				System.out.println("No WebCam ..................");
			}else{
				System.out.println("WebCamDisplay has a webcam to display");
				Component visu = _ply.getVisualComponent();
				//visu.validate();
				//System.out.println("visu.getSize()=");
				//this.setSize(visu.getSize());
				this.add(visu, webcamZidx);
				_ply.start();
			}
		}
		_TrackerSquare = new JPanel();
		_TrackerSquare.setBackground(new Color(0,255,0,127));
		_TrackerSquare.setSize(35, 35);
		this.validate();
		System.out.println("WebCamDisplay creation over");
	}
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */
	private void addCalibrationZone(){
		System.out.println("WebCDsp : addCalibrationZone");
		if(_ply != null){
			int height = this.getHeight();
			int width = this.getWidth();
			System.out.println("\t height = "+height+"\t width = "+width);
			double unitWidth = width/10;
			double unitHeight = height/10;
			int calibWidth = (int) (unitWidth*4);
			int calibHeight = (int) (unitHeight*4);
			int calibX = (int) (unitWidth*3);
			int calibY = (int) (unitHeight*3);
			System.out.println("\tcalib :"+calibWidth+"*"+calibHeight
					+"\tposition :"+calibX+"x"+calibY);
//			_calibrationArea = 
//				new HINBRect(calibWidth-10, calibHeight-10,
//						50, Color.RED);
			_calibrationArea = new JPanel();
			_calibrationArea.setBackground(Color.YELLOW);
			_calibrationArea.setSize(calibWidth, calibHeight);
			_calibrationArea.setLocation(calibX, calibY);
			//this.add(_calibrationArea, calibrationZidx);
			this.add(_calibrationArea, calibrationZidx);
			
			this.validate();
		}
	}
	private void removeCalibrationZone(){
		this.remove(_calibrationArea);
		this.validate();
	}
	public void setCalibrationZone(boolean on){
		if(on){
			this.addCalibrationZone();
		}else{
			this.removeCalibrationZone();
		}
	}
	
	/**
	 * getFrameGrabbingControl gets the FrameGrabbingControl of the player
	 * @return the FrameGrabbingControl of the player
	 */
	public FrameGrabbingControl getFrameGrabbingControl(){
		return (FrameGrabbingControl)_ply.getControl("javax.media.control.FrameGrabbingControl");
	}
	
	
	private void addTrackerSquare(){
		if(_ply != null){
			this.add(_TrackerSquare, trackingZidx);
			this.validate();
		}
	}
	private void removeTrackerSquare(){
		this.remove(_TrackerSquare);
		this.validate();
	}
	public void setTrackerSquare(boolean on){
		if(on){
			this.addTrackerSquare();
		}else{
			this.removeTrackerSquare();
		}
	}
	
	public void setTrackingPosition(int x, int y){
		_TrackerSquare.setLocation(x, y);
		this.validate();
	}
	public void setTrackingPosition(Point p){
		_TrackerSquare.setLocation(p);
		this.validate();
	}
	
	
	@Override
	public void validate() {
		System.out.println("WebCamDisplay validation !");
		super.validate();
	}
}
